//
//  ViewController.m
//  启动图片
//
//  Created by iOS on 16/6/11.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "ViewController.h"
#import "LanchImagesViewController.h"
@interface ViewController (){
BOOL _isClick;
}
@property (nonatomic,assign)BOOL lanchiamgeBool;
@property (nonatomic,strong)UIImageView * WelcomeAddes;
@property (nonatomic,strong)UILabel * timeLable;
@property (nonatomic,strong)NSTimer * waitTime;
@end
static NSInteger timeCount = 6;
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    /*****************/
    UIWindow * window = [[UIApplication sharedApplication]keyWindow] ;
  
    UIImageView *welcome = [[UIImageView alloc]initWithFrame:window.bounds];
    self.WelcomeAddes = welcome;
    NSLog(@"%@, %@",[self getLaunchImageName],window);
    //[welcome setImage:[UIImage imageNamed:[self getLaunchImageName]]];
    [welcome setImage:[UIImage imageNamed:@"qidong"]];
    //把背景图放在最上层
    
    [window addSubview:welcome];
    
    [window bringSubviewToFront:welcome];
    UILabel * lablel = [[UILabel alloc]initWithFrame:CGRectMake(window.bounds.size.width - 50, 30, 40, 20)];
    lablel.backgroundColor = [UIColor redColor];
    lablel.text = @"a";
    [window addSubview:lablel];

    self.timeLable = lablel;
    welcome.alpha = 0.99;
   self.waitTime = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeClick) userInfo:nil repeats:YES];
    [self performSelector:@selector(LanchImageClick) withObject:self afterDelay:timeCount];
    
    /*********放大淡出效果结束**********/
    
    /*********广告效果开始**********/
//    LanchImagesViewController * lanchImages  = [[LanchImagesViewController alloc]init];
//    lanchImages.myBlock = ^(BOOL isClick){
//        _isClick = isClick;
//    };
//    if (_isClick == NO) {
//       [self presentViewController:lanchImages animated:nil completion:nil];
//    }
    
    /*********广告效果开始**********/
}

- (void)LanchImageClick{
    UIWindow * window = [[UIApplication sharedApplication]keyWindow] ;

        [UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
//            CGRect frame = self.WelcomeAddes.frame;
//            frame.size.width = window.bounds.size.width*1.3;
//            frame.size.height = window.bounds.size.height*1.3;
//            self.WelcomeAddes.frame = frame;
            self.WelcomeAddes.center = window.center;
            self.WelcomeAddes.transform = CGAffineTransformMakeScale(1.3, 1.3);
            self.WelcomeAddes.alpha = 0;
    
        } completion:^(BOOL finished) {
    
            [self.WelcomeAddes removeFromSuperview];
        }];

}
- (NSString *)getLaunchImageName {
    
  //  CGSize viewSize = [[UIApplication sharedApplication]keyWindow].bounds.size;
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    // 竖屏
    NSString *viewOrientation = @"Portrait";
    NSString *launchImageName = nil;
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict) {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
            launchImageName = dict[@"UILaunchImageName"];
        }
    }
    return launchImageName;
}

- (void)timeClick{
    if (timeCount > 0) {
        timeCount--;
        self.timeLable.text = [NSString stringWithFormat:@"%ld",timeCount];

    }else{
        [self.waitTime invalidate];
        self.waitTime = nil;
        [self.timeLable removeFromSuperview];
    }
  }
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
